package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result save(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //通过拦截器获取当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //调用mapper获取购物车中是否有新添加菜品或套餐
        List<ShoppingCart> goodsList = shoppingCartMapper.getGoodsExsist(shoppingCart);
        //如果商品存在则将数量加1
        if (!CollectionUtils.isEmpty(goodsList)) {
            ShoppingCart goodsCart = goodsList.get(0);
            goodsCart.setNumber(goodsCart.getNumber() + 1);
            shoppingCartMapper.update(goodsCart);
            return Result.success();
        }
        //商品不存在则向购物车插入商品
        if(shoppingCartDTO.getDishId()!=null){
            Dish dish = dishMapper.getDish(shoppingCartDTO.getDishId());
            BeanUtils.copyProperties(dish, shoppingCart);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCartMapper.insert(shoppingCart);
            return Result.success();
        }else if(shoppingCartDTO.getSetmealId()!=null){
            Setmeal setmeal = setmealMapper.getSetmeal(shoppingCartDTO.getSetmealId());
            BeanUtils.copyProperties(setmeal, shoppingCart);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCartMapper.insert(shoppingCart);
            return Result.success();
        }else{
            return Result.error("请选择向购物车添加套餐或菜品信息");
        }
    }

    @Override
    public Result<List<ShoppingCart>> list() {
        Long userId = BaseContext.getCurrentId();
        //调用mapper获取购物车信息
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(userId);
        return Result.success(shoppingCartList);
    }

    @Override
    public Result clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
        return Result.success();
    }

    @Override
    public Result subNumber(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        //判断当前商品是否存在于购物车中
        List<ShoppingCart> goodsList = shoppingCartMapper.getGoodsExsist(shoppingCart);
        //如果该商品在购物车中且数量大于1则扣减数量
        if (!CollectionUtils.isEmpty(goodsList)&&goodsList.get(0).getNumber()>1) {
            ShoppingCart goodsCart = goodsList.get(0);
            goodsCart.setNumber(goodsCart.getNumber() - 1);
            shoppingCartMapper.update(goodsCart);
            return Result.success();
        }else if(!CollectionUtils.isEmpty(goodsList)&&goodsList.get(0).getNumber()==1) {
            shoppingCartMapper.delete(shoppingCart);
            return Result.success();
        }else{
            return Result.error("商品扣减数量失败");
        }
    }
}
